from django.db import models

class Ring(models.Model):
  """A competition ring.  Has information on all upcoming events"""
  number = models.IntegerField()

class EventType(models.Model):
  name = models.CharField(max_length=50)
  code = models.CharField(max_length=5)

class Level(models.Model):
  code = models.CharField(max_length=1)
  name = models.CharField(max_length=20)

class AgeGroup(models.Model):
  code = models.CharField(max_length=1)
  name = models.CharField(max_length=20)

class Gender(models.Model):
  code = models.CharField(max_length=1)
  name = models.CharField(max_length=20)

class Event(models.Model):
  ring = models.ForeignKey(Ring)
  order = models.IntegerField()
  event_type = models.ForeignKey(EventType)
  level = models.ForeignKey(Level)
  age_group = models.ForeignKey(AgeGroup)
  gender = models.ForeignKey(Gender)
  shortcode = models.CharField(max_length=6, primary_key=True)
  completed = models.BooleanField()  # Is false if ongoing
  ongoing = models.BooleanField()    # Is false if completed
  num_competitors = models.IntegerField()
  def __str__(self):
    return '%d, %s' % (self.ring_id, self.shortcode)

class Competitor(models.Model):
  """Competitor Information"""
  first_name = models.CharField(max_length=50)
  last_name = models.CharField(max_length=50)
  level = models.ForeignKey(Level, blank=True, null=True)
  age_group = models.ForeignKey(AgeGroup, blank=True, null=True)
  gender = models.ForeignKey(Gender, blank=True, null=True)
  events = models.ManyToManyField(Event)
  def __str__(self):
    return '%s %s' % (self.first_name, self.last_name)

class Placing(models.Model):
  """Links together competitor and event, stores placing information"""
  competitor = models.ForeignKey(Competitor)
  event = models.ForeignKey(Event)
  place = models.IntegerField(blank=True, null=True)
  # Their real score * 100, since we can't store floats
  scores = models.CommaSeparatedIntegerField(max_length=30, blank=True, null=True)
  order = models.IntegerField(blank=True, null=True)
  final_score = models.FloatField(blank=True, null=True)
  time = models.IntegerField(blank=True, null=True)  # In seconds
  # Again, this is scores * 100, so a 7.25 is 725
  time_deduct = models.IntegerField(blank=True, null=True)
  other_deduct = models.IntegerField(blank=True, null=True)

class AdminData(models.Model):
  """Misc data about the website"""
  last_updated = models.DateTimeField()
  secret = models.CharField(max_length=50)

EVENT_CATAGORIES = {
    100: 'Contemporary',
    200: 'Traditional',
    300: 'Internal',
    900: 'Push Hands',
    950: 'Group Sets',
}